﻿<?php 
function b() {print '<br/>';}

$search=$_GET["q"];
$count_printed_results=5;

$table=strtolower($_REQUEST["table"]);
if ($_SERVER["SERVER_NAME"]=="de.lingomoto.com")
      require_once("konfiguration.html");
elseif ($_SERVER["SERVER_NAME"]=="es.lingomoto.com")
		 require_once("configuracion.html");
		 
else
{
	if ($_SERVER["SERVER_NAME"]=="localhost")
		require_once("konfiguration.html");
	else 
		require_once("configuration.html");
}
 

$result=mysql_query("SELECT canword FROM $table WHERE canword like '{$search}%' ORDER BY canword LIMIT 15");
//print "SELECT canword FROM $table WHERE canword like '{$search}%' ORDER BY canword LIMIT 15";
//could not use DISTINCT because of utf8_unicode_ci collation (case insensitive and finds umlauts, when
//vowel given, as well as any vowel with accent,finds ß on ss, etc... )  
while ($objRow=mysql_fetch_array($result)) $arrPreResult[]=$objRow["canword"];
if (is_array($arrPreResult))
{
	$arrResult=array_unique ($arrPreResult); 
	//cleans up array from all the rows which have the 2nd, 3rd translation in it 
	if ($intPos=array_search($search, $arrResult)) //the input is  
	{
		$arrResult[$intPos]="";
		array_unshift($arrResult,$search);
	}
}
$intResultCount= count($arrResult);

//if there arent enough words which match from the beginning of the word, also give words that match inbetween 
if (($intResultCount < $count_printed_results))
{
     $diff= ($count_printed_results - $intResultCount)*2;
	 $result2=mysql_query("SELECT canword FROM $table WHERE canword like '%{$search}%' AND canword NOT LIKE '{$search}%' ORDER BY canword LIMIT {$diff} ");
	 while ($objRow=mysql_fetch_array($result2)) $arrRemainderPreResult[]=$objRow["canword"];
	 if (is_array($arrRemainderPreResult))
	 {
	 	$arrRemainderResult=array_unique($arrRemainderPreResult);
	 	if (is_array($arrResult)) $arrResult=array_merge($arrResult,$arrRemainderResult);
	 	else $arrResult=$arrRemainderResult;
	 }  	 
}
if (is_array($arrResult))
{
	$arrResult=array_values($arrResult);
	for($count = 0; $count <= $count_printed_results; $count++)
	     print $arrResult[$count]."\n";
} 

?>
